import { PrismaService } from '@app/prisma';
import { ClassesEntity } from '@app/prisma/classes.entity/classes.entity';
import { CollegeEntity } from '@app/prisma/college.entity/college.entity';
import { ScienceEntity } from '@app/prisma/science.entity/science.entity';
import { UseGuards } from '@nestjs/common';
import { Args, Int, Mutation,  Resolver } from '@nestjs/graphql';
import { AccountPower,  GqlAuthPowerGuard } from 'src/auth/auth.guard';
import { CommonService } from './common.service';
import { DepartmentEntity } from '@app/prisma/department.entity/department.entity';

@Resolver()
export class CommonResolver {
    constructor(
        private readonly prisma: PrismaService,
        private readonly commonService: CommonService
    ) {

    }
    @Mutation(() => CollegeEntity, { nullable: true })
    @UseGuards(new GqlAuthPowerGuard(AccountPower))
    async delCollege(
        @Args("id", { type: () => Int }) id: number
    ) {
        const college: CollegeEntity = await this.prisma.college.findUnique({
            where: {
                id
            },
            include: {
                Department: {
                    include: {
                        Science: {
                            include: {
                                Classes: true
                            }
                        }
                    }
                }
            }
        });
        await this.commonService.delColleges([college])
        return null;
    }


    @Mutation(() => DepartmentEntity, { nullable: true })
    @UseGuards(new GqlAuthPowerGuard(AccountPower))
    async delDepartment(
        @Args("id", { type: () => Int }) id: number
    ) {
        const dept = await this.prisma.department.findUnique({
            where: {
                id
            },
            include: {
                Science: {
                    include: {
                        Classes: true
                    }
                }
            }
        });
        await this.commonService.delDepartment([dept])
        return null;
    }

    @Mutation(() => ScienceEntity, { nullable: true })
    @UseGuards(new GqlAuthPowerGuard(AccountPower))
    async delScience(
        @Args("id", { type: () => Int }) id: number
    ) {
        const science = await this.prisma.science.findUnique({
            where: {
                id
            },
            include: {
                Classes: true
            }
        });
        await this.commonService.delSciences([science])
        return null;
    }

    @Mutation(() => ClassesEntity, { nullable: true })
    @UseGuards(new GqlAuthPowerGuard(AccountPower))
    delClasses(
        @Args("id", { type: () => Int }) id: number
    ) {
        return this.prisma.classes.delete({
            where: {
                id
            }
        })
    }
}
